package com.TanNgee.demo3;

import java.util.concurrent.Executors;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.TimeUnit;

/**
 * @author 陈毅
 * @create 2022/4/25 22:43
 * 测试ScheduleExecutorService接口中延迟执行任务和重复执行任务的功能
 */
public class ScheduleExecutorServiceDemo03 {
    public static void main(String[] args) {
        // 1 获取一个具备延迟执行任务的线程池对象
        ScheduledExecutorService es = Executors.newSingleThreadScheduledExecutor(new ThreadFactory() {
            int n = 1;

            @Override
            public Thread newThread(Runnable r) {
                return new Thread(r, "自定义线程名：" + n++);
            }
        });
        // 2 创建多个任务对象，提交任务，每个任务延迟2秒执行
//        for (int i = 0; i < 10; i++) {
//        创建并执行一个在给定初始延迟后首次启用的定期操作，随后，在每一次执行终止和下一次执行开始之间都存在给定的延迟。
        es.scheduleWithFixedDelay(new MyRunnable3(1), 1, 2, TimeUnit.SECONDS);
//        }
        System.out.println("over");
    }
}

class MyRunnable3 implements Runnable {
    private int id;

    public MyRunnable3(int id) {
        this.id = id;
    }

    @Override
    public void run() {
        final String name = Thread.currentThread().getName();
        try {
            Thread.sleep(2000);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        }
        System.out.println(name + "执行了任务：" + id);
    }
}

